package com.lizhi.yun.core.patient;/**
 * @program: hbsf_yun
 * @name MsbPatientHandler
 * @description:
 * @author: lizhi
 * @create: 2021-12-31 13:19
 */

import com.lizhi.common.yun.entity.RpcPatientPOJO;
import com.lizhi.common.yun.entity.RpcServicePOJO;
import com.lizhi.common.yun.enums.HeartBeartEnum;
import com.lizhi.utils.StringUtils;
import com.lizhi.utils.cache.MemoryCacheUtil;
import com.lizhi.utils.json.SamJsonUtil;
import com.lizhi.yun.db.msb.dao.MsbPatientDao;
import com.lizhi.yun.db.msb.entity.MsbPatientEntity;
import com.lizhi.yun.deploy.NewBaseHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 *@program: hbsf_yun
 *@name MsbPatientHandler
 *@description: 患者信息
 *@author: lizhi
 *@create: 2021-12-31 13:19
 */
@Slf4j
@Service
public class MsbPatientHandler extends NewBaseHandler {

    @Resource
    private MsbPatientDao msbPatientDao;

    /**
     *功能描述 接收患者信息
     * @author lizhi
     * @date 2021/12/31
     * @param json:
     * @return java.lang.String
     */
    public String recvPatient(String json){
        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        try {
            MsbPatientEntity msbPatientEntity = SamJsonUtil.toObject(json, MsbPatientEntity.class);
            MsbPatientEntity patientEntity = msbPatientDao.selectById(msbPatientEntity.ptId, msbPatientEntity.hospitalId, msbPatientEntity.hospitalSystemId);
            if (StringUtils.isEmpty(patientEntity)){
                msbPatientEntity.lid = StringUtils.newId();
                msbPatientDao.insertRecord(msbPatientEntity);
            } else {
                msbPatientDao.updateByIdSelective(msbPatientEntity);
            }
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        } catch (Exception e){
            log.error("接收同步的患者信息出现异常:{}", e.getMessage());
        }
        return SamJsonUtil.toJson(rpcServicePOJO);
    }

    /**
     *功能描述 接收到正在训练的患者信息
     * @author lizhi
     * @date 2021/12/31
     * @param json:
     * @return java.lang.String
     */
    public String trainingPatient(String json){
        RpcServicePOJO rpcServicePOJO = new RpcServicePOJO();
        try {
            MsbPatientEntity msbPatientEntity = SamJsonUtil.toObject(json, MsbPatientEntity.class);
            RpcPatientPOJO rpcPatientPOJO = new RpcPatientPOJO();
            rpcPatientPOJO.setPtId(msbPatientEntity.ptId);
            rpcPatientPOJO.setPtNm(msbPatientEntity.ptNm);
            rpcPatientPOJO.setPtSex(msbPatientEntity.ptSex);
            rpcPatientPOJO.setPtSexText(msbPatientEntity.ptSexText);
            rpcPatientPOJO.setPtAge(msbPatientEntity.ptAge);
            rpcPatientPOJO.setPtPhone(msbPatientEntity.ptPhone);
            rpcPatientPOJO.setHospitalId(msbPatientEntity.getHospitalId());
            rpcPatientPOJO.setHospitalSystemId(msbPatientEntity.getHospitalSystemId());
            /*放入到缓存中*/
            MemoryCacheUtil.put(rpcPatientPOJO.getHospitalId() + "-" + rpcPatientPOJO.getHospitalSystemId(), rpcPatientPOJO, 40000);
            rpcServicePOJO.setErrorCode(HeartBeartEnum.HEART_BEART_000000.getEnValue());
            rpcServicePOJO.setMessage(HeartBeartEnum.HEART_BEART_000000.getEnName());
        } catch (Exception e){
            log.error("接收同步的正在训练患者信息出现异常:{}", e.getMessage());
        }
        return SamJsonUtil.toJson(rpcServicePOJO);
    }
}
